// var i;
// var $bundleConfig = require("./BundleConfig");
// var $resKeeper = require("./ResKeeper");
// var $excel = require("./Excel");
// var $gameGlobalVariable = require("./GameGlobalVariable");
// var $nodePoolMag = require("./NodePoolMag");
// var $uI_MosterLogic = require("./UI_MosterLogic");
// var $uI_BulletCollider = require("./UI_BulletCollider");
// var _ = cc._decorator;
// var y = _.ccclass;
// var m = _.property;
// var v = _.inspector;
// var b = (function (e) {
//     function t() {
//         var t = (null !== e && e.apply(this, arguments)) || this;
//         t.bulletId = 1;
//         t.img = null;
//         t.attack = 0;
//         t.isbaoji = !1;
//         t.attackfang = null;
//         t.attacktype = 1;
//         t.nowtagMoster = null;
//         t.nowdirection = 0;
//         t.tuoweiNode = null;
//         t.tagMosterX = 0;
//         t.tagMosterY = 0;
//         t.isshua = !1;
//         t.offtime = 0;
//         t.bulletspeed = 20;
//         t.issend = !1;
//         return t;
//     }
//     __extends(t, e);
//     t.prototype.initBullet = function (e, t) {
//         return __awaiter(this, void 0, void 0, function () {
//             var o;
//             var n;
//             var i;
//             var a;
//             var r;
//             return __generator(this, function (c) {
//                 switch (c.label) {
//                     case 0:
//                         this.attack = e.attack;
//                         this.isshua = !1;
//                         this.attackfang = e.playerId;
//                         this.attacktype = e.feixintype;
//                         this.isbaoji = e.isbaoji;
//                         this.nowtagMoster = t;
//                         o = cc.v2();
//                         cc.Vec2.subtract(o, cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//                         this.nowdirection = Math.atan2(o.y, o.x);
//                         this.issend = !0;
//                         if ((n = $excel.Excel.shuiguobuff(this.attackfang.sgtianfuid))) {
//                             switch (n.buffefftype) {
//                                 case 10:
//                                     i = cc.Vec2.distance(
//                                         cc.v2(this.nowtagMoster.x, this.nowtagMoster.y),
//                                         this.node.getPosition()
//                                     );
//                                     a = (i / n.value[0]) * (n.value[1] / 100);
//                                     this.attack = this.attack + this.attack * a;
//                             }
//                         }
//                         return [
//                             4,
//                             this._loadRes(
//                                 $bundleConfig.BundleNames.Common,
//                                 "res/player/player" + this.attackfang.playerId + "_1",
//                                 cc.SpriteFrame
//                             )
//                         ];
//                     case 1:
//                         r = c.sent();
//                         this.img.spriteFrame = r;
//                         this.createTuoWei();
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.onDisable = function () {
//         if (this.tuoweiNode) {
//             $nodePoolMag.NodePoolMag.instance.recycleNode("tuowei", this.tuoweiNode);
//             this.tuoweiNode = null;
//         }
//     };
//     t.prototype.createTuoWei = function () {
//         return __awaiter(this, void 0, void 0, function () {
//             var e;
//             var t;
//             return __generator(this, function (o) {
//                 switch (o.label) {
//                     case 0:
//                         if ((e = $nodePoolMag.NodePoolMag.instance.getNode("tuowei"))) {
//                             return [3, 2];
//                         } else {
//                             return [
//                                 4,
//                                 this._loadRes($bundleConfig.BundleNames.Game, "prefabs/effpre/UI_TuoWei", cc.Prefab)
//                             ];
//                         }
//                     case 1:
//                         t = o.sent();
//                         $nodePoolMag.NodePoolMag.instance.createNodeCount("tuowei", 1, t);
//                         e = $nodePoolMag.NodePoolMag.instance.getNode("tuowei");
//                         o.label = 2;
//                     case 2:
//                         this.tuoweiNode = e;
//                         $gameGlobalVariable.GameGlobalVariable.effNode.addChild(e);
//                         return [2];
//                 }
//             });
//         });
//     };
//     t.prototype.getBulletAttack = function (e) {
//         var t = this.attack;
//         if (e.isjianshu && 14 == this.attackfang.playerId) {
//             var o = $excel.Excel.shuiguo(1014);
//             t += t * ($excel.Excel.shuiguobuff(o.talent).value[0] / 100);
//         }
//         if (1 == e.nowmosterdata.boss && 10 == this.attackfang.playerId) {
//             o = $excel.Excel.shuiguo(1010);
//             t += t * ($excel.Excel.shuiguobuff(o.talent).value[0] / 100);
//         }
//         return t;
//     };
//     t.prototype.sendBullet1 = function (e) {
//         var t = cc.v2();
//         if (!this.isshua) {
//             this.tagMosterX = this.nowtagMoster.x;
//             this.tagMosterY = this.nowtagMoster.y;
//         }
//         cc.Vec2.subtract(t, cc.v2(this.tagMosterX, this.tagMosterY), this.node.getPosition());
//         this.nowdirection = Math.atan2(t.y, t.x);
//         this.moveBullet(this.nowdirection, e);
//     };
//     t.prototype.moveBullet = function (e, t) {
//         this.node.x += Math.cos(e) * this.bulletspeed;
//         this.node.y += Math.sin(e) * this.bulletspeed;
//         if (this.nowtagMoster.getComponent($uI_MosterLogic.default).isDie) {
//             this.isshua = !0;
//         }
//         var o = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//         this.offtime += t;
//         if (o <= 25) {
//             this.img.node.getComponent($uI_BulletCollider.default).onYouHuaPeng(this.nowtagMoster);
//         }
//         if (this.isshua) {
//             var n = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//             this.offtime += t;
//             if (n <= 50) {
//                 this.hideBullet();
//             }
//             if (this.offtime >= 0.5) {
//                 this.offtime = 0;
//                 this.hideBullet();
//             }
//         }
//     };
//     t.prototype.hideBullet = function () {
//         this.issend = !1;
//         $nodePoolMag.NodePoolMag.instance.recycleNode("bullet_" + this.bulletId, this.node);
//     };
//     t.prototype.getNowMosterNode = function (e) {
//         var t = this.attackfang.mosterNodes.children;
//         var o = [];
//         for (var n = 0; n < t.length; n++) {
//             var i = t[n];
//             var a = cc.Vec2.distance(i.getPosition(), this.node.getPosition());
//             if ((a -= i.width / 2 + this.node.width / 2) <= e) {
//                 o.push(i);
//             }
//         }
//         return o;
//     };
//     t.prototype.sendBullet2 = function () {};
//     t.prototype.getBulletSpeed = function () {
//         this.bulletspeed = this.bulletspeed * $gameGlobalVariable.GameGlobalVariable.jiasuState;
//         return this.bulletspeed;
//     };
//     t.prototype.onUpdate = function (e) {
//         if (this.issend) {
//             this.sendBullet1(e);
//             if (this.tuoweiNode) {
//                 this.tuoweiNode.x = this.node.x;
//                 this.tuoweiNode.y = this.node.y;
//             }
//         }
//     };
//     __decorate([m()], t.prototype, "bulletId", void 0);
//     __decorate([m(cc.Sprite)], t.prototype, "img", void 0);
//     return __decorate([y], t);
// })($resKeeper.default);
// exports.default = b;


// import {BundleNames} from "./BundleConfig";
// import ResKeeper from "./ResKeeper";

// import { GameGlobalVariable } from "./GameGlobalVariable";
// import { NodePoolMag } from "./NodePoolMag";
// import UI_MosterLogic from "./UI_MosterLogic";
// import UI_BulletCollider from "./UI_BulletCollider";
// import { Excel } from './Excel';

// const { ccclass, property, inspector } = cc._decorator;

// /**
//  * 建议类名: Bullet
//  */
// @ccclass
// class UI_Bullet extends ResKeeper {
//     @property()
//     bulletId: number = 1;

//     @property(cc.Sprite)
//     img: cc.Sprite = null;

//     attack: number = 0;
//     isbaoji: boolean = false;
//     attackfang: any = null;
//     attacktype: number = 1;
//     nowtagMoster: any = null;
//     nowdirection: number = 0;
//     tuoweiNode: cc.Node = null;
//     tagMosterX: number = 0;
//     tagMosterY: number = 0;
//     isshua: boolean = false;
//     offtime: number = 0;
//     bulletspeed: number = 20;
//     issend: boolean = false;

//     constructor() {
//         super();
//     }

//     /**
//      * 初始化子弹
//      * @param e 子弹属性
//      * @param t 目标怪物
//      * 建议方法名: initializeBullet
//      */
//     async initBullet(e: any, t: any): Promise<void> {
//         this.attack = e.attack;
//         this.isshua = false;
//         this.attackfang = e.playerId;
//         this.attacktype = e.feixintype;
//         this.isbaoji = e.isbaoji;
//         this.nowtagMoster = t;

//         const directionVector = cc.v2();
//         cc.Vec2.subtract(directionVector, cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//         this.nowdirection = Math.atan2(directionVector.y, directionVector.x);
//         this.issend = true;

//         const buff = Excel.shuiguobuff(this.attackfang.sgtianfuid);
//         if (buff) {
//             switch (buff.buffefftype) {
//                 case 10:
//                     const distance = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//                     const attackIncrease = (distance / buff.value[0]) * (buff.value[1] / 100);
//                     this.attack += this.attack * attackIncrease;
//                     break;
//             }
//         }

//         const spriteFrame = await this._loadRes(BundleNames.Common, `res/player/player${this.attackfang.playerId}_1`, cc.SpriteFrame);
//         this.img.spriteFrame = spriteFrame;
//         this.createTuoWei();
//     }

//     /**
//      * 当节点禁用时调用
//      * 建议方法名: onNodeDisable
//      */
//     onDisable(): void {
//         if (this.tuoweiNode) {
//             NodePoolMag.instance.recycleNode("tuowei", this.tuoweiNode);
//             this.tuoweiNode = null;
//         }
//     }

//     /**
//      * 创建拖尾效果
//      * 建议方法名: createTrailEffect
//      */
//     async createTuoWei(): Promise<void> {
//         let trailNode = NodePoolMag.instance.getNode("tuowei");
//         if (!trailNode) {
//             const prefab = await this._loadRes(BundleNames.Game, "prefabs/effpre/UI_TuoWei", cc.Prefab);
//             NodePoolMag.instance.createNodeCount("tuowei", 1, prefab);
//             trailNode = NodePoolMag.instance.getNode("tuowei");
//         }
//         this.tuoweiNode = trailNode;
//         GameGlobalVariable.effNode.addChild(trailNode);
//     }

//     /**
//      * 获取子弹攻击力
//      * @param e 目标怪物数据
//      * 建议方法名: calculateBulletAttack
//      */
//     getBulletAttack(e: any): number {
//         let attackValue = this.attack;
//         if (e.isjianshu && this.attackfang.playerId === 14) {
//             const buff = Excel.shuiguo(1014);
//             attackValue += attackValue * (Excel.shuiguobuff(buff.talent).value[0] / 100);
//         }
//         if (e.nowmosterdata.boss === 1 && this.attackfang.playerId === 10) {
//             const buff = Excel.shuiguo(1010);
//             attackValue += attackValue * (Excel.shuiguobuff(buff.talent).value[0] / 100);
//         }
//         return attackValue;
//     }

//     /**
//      * 发送子弹1
//      * @param e 时间间隔
//      * 建议方法名: sendBulletType1
//      */
//     sendBullet1(e: number): void {
//         const directionVector = cc.v2();
//         if (!this.isshua) {
//             this.tagMosterX = this.nowtagMoster.x;
//             this.tagMosterY = this.nowtagMoster.y;
//         }
//         cc.Vec2.subtract(directionVector, cc.v2(this.tagMosterX, this.tagMosterY), this.node.getPosition());
//         this.nowdirection = Math.atan2(directionVector.y, directionVector.x);
//         this.moveBullet(this.nowdirection, e);
//     }

//     /**
//      * 移动子弹
//      * @param direction 子弹方向
//      * @param deltaTime 时间间隔
//      * 建议方法名: moveBullet
//      */
//     moveBullet(direction: number, deltaTime: number): void {
//         this.node.x += Math.cos(direction) * this.bulletspeed;
//         this.node.y += Math.sin(direction) * this.bulletspeed;

//         if (this.nowtagMoster.getComponent(UI_MosterLogic).isDie) {
//             this.isshua = true;
//         }

//         const distanceToTarget = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//         this.offtime += deltaTime;

//         if (distanceToTarget <= 25) {
//             this.img.node.getComponent(UI_BulletCollider).onYouHuaPeng(this.nowtagMoster);
//         }

//         if (this.isshua) {
//             const distanceToTarget = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
//             this.offtime += deltaTime;
//             if (distanceToTarget <= 50) {
//                 this.hideBullet();
//             }
//             if (this.offtime >= 0.5) {
//                 this.offtime = 0;
//                 this.hideBullet();
//             }
//         }
//     }

//     /**
//      * 隐藏子弹
//      * 建议方法名: hideBullet
//      */
//     hideBullet(): void {
//         this.issend = false;
//         NodePoolMag.instance.recycleNode(`bullet_${this.bulletId}`, this.node);
//     }

//     /**
//      * 获取当前怪物节点
//      * @param range 范围
//      * 建议方法名: getCurrentMonsterNodes
//      */
//     getNowMosterNode(range: number): cc.Node[] {
//         const monsterNodes = this.attackfang.mosterNodes.children;
//         const result: cc.Node[] = [];
//         for (let i = 0; i < monsterNodes.length; i++) {
//             const monsterNode = monsterNodes[i];
//             const distance = cc.Vec2.distance(monsterNode.getPosition(), this.node.getPosition());
//             if ((distance - (monsterNode.width / 2 + this.node.width / 2)) <= range) {
//                 result.push(monsterNode);
//             }
//         }
//         return result;
//     }

//     /**
//      * 发送子弹2
//      * 建议方法名: sendBulletType2
//      */
//     sendBullet2(): void { }

//     /**
//      * 获取子弹速度
//      * 建议方法名: getBulletSpeed
//      */
//     getBulletSpeed(): number {
//         this.bulletspeed *= GameGlobalVariable.jiasuState;
//         return this.bulletspeed;
//     }

//     /**
//      * 更新方法
//      * @param deltaTime 时间间隔
//      * 建议方法名: update
//      */
//     onUpdate(deltaTime: number): void {
//         if (this.issend) {
//             this.sendBullet1(deltaTime);
//             if (this.tuoweiNode) {
//                 this.tuoweiNode.x = this.node.x;
//                 this.tuoweiNode.y = this.node.y;
//             }
//         }
//     }
// }

// export default UI_Bullet;


import {BundleNames} from "./BundleConfig";
import ResKeeper from "./ResKeeper";
import { GameGlobalVariable } from "./GameGlobalVariable";
import { NodePoolMag } from "./NodePoolMag";
import ViewMosterLogic from "./UI_MosterLogic";
import ViewBulletCollider from "./UI_BulletCollider";
import { Excel } from './Excel';

// Cocos Creator 装饰器
const { ccclass, property } = cc._decorator;

/**
 * 建议类名: Bullet
 */
@ccclass
class ViewBullet extends ResKeeper {
    @property()
    bulletId: number = 1;

    @property(cc.Sprite)
    img: cc.Sprite = null;

//     attack: number = 0;
//     isbaoji: boolean = false;
//     attackfang: any = null;
//     attacktype: number = 1;
//     nowtagMoster: any = null;
//     nowdirection: number = 0;
//     tuoweiNode: cc.Node = null;
//     tagMosterX: number = 0;
//     tagMosterY: number = 0;
//     isshua: boolean = false;
//     offtime: number = 0;
//     bulletspeed: number = 20;
//     issend: boolean = false;


    attack: number = 0;
    isbaoji: boolean = false;
    attackfang: any = null;
    attacktype: number = 1;
    nowtagMoster: any = null;
    nowdirection: number = 0;
    tuoweiNode: cc.Node = null;
    tagMosterX: number = 0;
    tagMosterY: number = 0;
    isshua: boolean = false;
    offtime: number = 0;
    bulletspeed: number = 20;
    issend: boolean = false;

    constructor() {
        super();
    }

    /**
     * 初始化子弹
     * @param bulletProperties 子弹属性
     * @param target 目标怪物
     * 建议方法名: initializeBullet
     */
    async initializeBullet(bulletProperties: any, target: any): Promise<void> {
        this.attack = bulletProperties.attack;
        this.isshua = false;
        this.attackfang = bulletProperties.playerId;
        this.attacktype = bulletProperties.feixintype;
        this.isbaoji = bulletProperties.isbaoji;
        this.nowtagMoster = target;

        const directionVector = cc.v2();
        cc.Vec2.subtract(directionVector, cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
        this.nowdirection = Math.atan2(directionVector.y, directionVector.x);
        this.issend = true;

        const buff = Excel.shuiguobuff(this.attackfang.sgtianfuid);
        if (buff) {
            switch (buff.buffefftype) {
                case 10:
                    const distance = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
                    const attackIncrease = (distance / buff.value[0]) * (buff.value[1] / 100);
                    this.attack += this.attack * attackIncrease;
                    break;
            }
        }

        const spriteFrame = await this._loadRes(BundleNames.Common, `res/player/player${this.attackfang.playerId}_1`, cc.SpriteFrame);
        this.img.spriteFrame = spriteFrame;
        this.createTrailEffect();
    }

    /**
     * 当节点禁用时调用
     * 建议方法名: onNodeDisable
     */
    onDisable(): void {
        if (this.tuoweiNode) {
            NodePoolMag.instance.recycleNodeToPool("tuowei", this.tuoweiNode);
            this.tuoweiNode = null;
        }
    }

    /**
     * 创建拖尾效果
     * 建议方法名: createTrailEffect
     */
    async createTrailEffect(): Promise<void> {
        let node = NodePoolMag.instance.getNodeFromPool("tuowei");
        if (!node) {
            const prefab = await this._loadRes(BundleNames.Game, "prefabs/effpre/UI_TuoWei", cc.Prefab);
            NodePoolMag.instance.createNodePool("tuowei", 1, prefab);
            node = NodePoolMag.instance.getNodeFromPool("tuowei");
        }
        this.tuoweiNode = node;
        GameGlobalVariable.effNode.addChild(node);
    }

    /**
     * 获取子弹攻击力
     * @param monsterData 目标怪物数据
     * 建议方法名: calculateBulletAttack
     */
    getBulletAttack(monsterData: any): number {
        let attack = this.attack;
        if (monsterData.isjianshu && this.attackfang.playerId === 14) {
            const buff = Excel.shuiguo(1014);
            attack += attack * (Excel.shuiguobuff(buff.talent).value[0] / 100);
        }
        if (monsterData.nowmosterdata.boss === 1 && this.attackfang.playerId === 10) {
            const buff = Excel.shuiguo(1010);
            attack += attack * (Excel.shuiguobuff(buff.talent).value[0] / 100);
        }
        return attack;
    }

    /**
     * 发送子弹1
     * @param deltaTime 时间间隔
     * 建议方法名: sendBulletType1
     */
    sendBulletType1(deltaTime: number): void {
        const directionVector = cc.v2();
        if (!this.isshua) {
            this.tagMosterX = this.nowtagMoster.x;
            this.tagMosterY = this.nowtagMoster.y;
        }
        cc.Vec2.subtract(directionVector, cc.v2(this.tagMosterX, this.tagMosterY), this.node.getPosition());
        this.nowdirection = Math.atan2(directionVector.y, directionVector.x);
        this.moveBullet(this.nowdirection, deltaTime);
    }

    /**
     * 移动子弹
     * @param direction 子弹方向
     * @param deltaTime 时间间隔
     * 建议方法名: moveBullet
     */
    moveBullet(direction: number, deltaTime: number): void {
        this.node.x += Math.cos(direction) * this.bulletspeed;
        this.node.y += Math.sin(direction) * this.bulletspeed;

        if (this.nowtagMoster.getComponent(ViewMosterLogic).isDie) {
            this.isshua = true;
        }

        const distanceToTarget = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
        this.offtime += deltaTime;

        if (distanceToTarget <= 25) {
            this.img.node.getComponent(ViewBulletCollider).onOptimizedCollision(this.nowtagMoster);
        }

        if (this.isshua) {
            const distanceToTarget = cc.Vec2.distance(cc.v2(this.nowtagMoster.x, this.nowtagMoster.y), this.node.getPosition());
            this.offtime += deltaTime;
            if (distanceToTarget <= 50) {
                this.hideBullet();
            }
            if (this.offtime >= 0.5) {
                this.offtime = 0;
                this.hideBullet();
            }
        }
    }

    /**
     * 隐藏子弹
     * 建议方法名: hideBullet
     */
    hideBullet(): void {
        this.issend = false;
        NodePoolMag.instance.recycleNodeToPool(`bullet_${this.bulletId}`, this.node);
    }

    /**
     * 获取当前怪物节点
     * @param range 范围
     * 建议方法名: getCurrentMonsterNodes
     */
    getCurrentMonsterNodes(range: number): cc.Node[] {
        const monsterNodes = this.attackfang.mosterNodes.children;
        const result: cc.Node[] = [];
        for (let i = 0; i < monsterNodes.length; i++) {
            const monsterNode = monsterNodes[i];
            const distance = cc.Vec2.distance(monsterNode.getPosition(), this.node.getPosition());
            if ((distance - (monsterNode.width / 2 + this.node.width / 2)) <= range) {
                result.push(monsterNode);
            }
        }
        return result;
    }

    /**
     * 发送子弹2
     * 建议方法名: sendBulletType2
     */
    sendBulletType2(): void { }

    /**
     * 获取子弹速度
     * 建议方法名: getBulletSpeed
     */
    getBulletSpeed(): number {
        this.bulletspeed *= GameGlobalVariable.jiasuState;
        return this.bulletspeed;
    }

    /**
     * 更新方法
     * @param deltaTime 时间间隔
     * 建议方法名: update
     */
    onUpdate(deltaTime: number): void {
        if (this.issend) {
            this.sendBulletType1(deltaTime);
            if (this.tuoweiNode) {
                this.tuoweiNode.x = this.node.x;
                this.tuoweiNode.y = this.node.y;
            }
        }
    }
}

export default ViewBullet;